package behaviors;

import model.Assets;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.contacts.ContactPoint;
import org.jbox2d.dynamics.contacts.ContactResult;
import controller.Global;

public class Splash extends Behavior {

	@Override
	public void onCollide(ContactPoint arg0) {
	}

	@Override
	public void update(Body b) {
		super.update(b);

		float distance = (float) Math.sqrt((double) Math.pow(Math.abs(body
				.getPosition().x
				- Global.player.getPosition().x), 2)
				+ Math.pow(Math.abs(body.getPosition().y
						- Global.player.getPosition().y), 2));
		distance /= 10;

		// generate splash sound
		if (body.getPosition().y < Global.waterLevel && !entity.underWater) {

			Assets.getInstance().getSound("assets/sfx/splash.wav").play(
					1,
					(float) (Math.abs(body.getLinearVelocity().y / 20) / Math
							.pow(distance, 2)));

			if (Global.player.equals(body)) {
				Assets.getInstance().getSound("assets/sfx/underwater.wav")
						.loop();
			}

			entity.underWater = true;

			Global.water.impact(Global.worldToScreen(body.getPosition().x, body
					.getPosition().y).x, body.getLinearVelocity().y);
		}
		if (body.getPosition().y > Global.waterLevel
				&& entity.underWater != false) {

			Assets.getInstance().getSound("assets/sfx/splash.wav").play(
					1,
					(float) (Math.abs(body.getLinearVelocity().y / 20) / Math
							.pow(distance, 2)) / 10);

			entity.underWater = false;

			Global.water.impact(Global.worldToScreen(body.getPosition().x, body
					.getPosition().y).x, body.getLinearVelocity().y);

			if (Global.player.equals(body)) {
				Assets.getInstance().getSound("assets/sfx/underwater.wav")
						.stop();
			}
		}
	}

	@Override
	public void onRemove(ContactPoint arg0) {
	}

	@Override
	public void onPersist(ContactPoint arg0) {
	}

	@Override
	public void onResult(ContactResult arg0) {
	}

	@Override
	public void init() {
	}
}
